package com.example.androidlearning.sy13;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.view.ViewGroup;
import android.widget.SeekBar;

import androidx.annotation.Nullable;

import java.io.IOException;

public class MusicService extends Service {

    MyReceiver serviceReceiver;
    AssetManager am;
    String[] songs;
    MediaPlayer player;
    SeekBar seekBar;

    int status;
    int current;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        init();
        am =getAssets();
        serviceReceiver = new MyReceiver();

        IntentFilter filter = new IntentFilter();
        filter.addAction(BroadcastActivity.CTL_ACTION);
        registerReceiver(serviceReceiver, filter);
        player = new MediaPlayer();

        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                current++;
                if(current >= 3){
                    current = 0;
                }
                Intent sendIntent = new Intent(BroadcastActivity.UPDATE_ACTION);
                sendIntent.putExtra("current", current);
                sendBroadcast(sendIntent);

            }
        });
    }

    private void init(){
        // main/assets目录下歌曲的完整名字
        songs = new String[]{"bocaixiaomiwowotou.mp3"};
        status = 0x11;

    }

    public class MyReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            int control = intent.getIntExtra("control", -1);
            switch (control){
                case 1:
                    if(status == 0x11){
                        prepareAndPlay(songs[current]);
                        status = 0x12;
                    }else if(status == 0x12){
                        player.pause();
                        status = 0x13;
                    }else if(status == 0x13){
                        player.start();
                        status = 0x12;
                    }
                    break;
                case 2:
                    if(status == 0x12 || status == 0x13){
                        player.stop();
                        status = 0x11;
                    }
                    break;
            }
            Intent sendIntent = new Intent(BroadcastActivity.UPDATE_ACTION);
            sendIntent.putExtra("update", status);
            sendIntent.putExtra("current", current);
            sendBroadcast(sendIntent);
        }
    }

    private void prepareAndPlay(String music){
        try {
            AssetFileDescriptor afd = am.openFd(music);
            player.reset();
            player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
            player.prepare();
            player.start();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
